﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BaMusaSoft.RPS.DataModel.Services
{
    class Helper
    {
        internal static int StartNewIncrement(string currentYear)
        {
            int temp;
            if (! int.TryParse(currentYear, out temp)) // check for invalid year 
            {
                throw new InvalidCastException("Current Year must be number only");
            }
            string s = currentYear + "0001";
            return int.Parse(s);
        }

        internal static bool IsValidDate(string dateString)
        {
            bool result = false;
            if (string.IsNullOrEmpty(dateString)) { return false; }
            if (dateString.Length != 8) { return false; }
            string d = dateString.Substring(6, 2);
            string m = dateString.Substring(4, 2);
            string y = dateString.Substring(0, 4);

            int day;
            int month;
            int year;

            if (int.TryParse(d, out day) &&
                int.TryParse(m, out month) &&
                int.TryParse(y, out year))
            {
                if (
                     (day > 0 && day < 31)
                     &&
                     (month > 0 && month <= 12)
                     &&
                     (year > 1400))
                {
                    result = true;
                }
            }
            return result;
        }
        internal static bool IsValidEmail(string email)
        { 
             string pattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
             Regex reg = new Regex(pattern);
            if(!string.IsNullOrEmpty(email))
            {
             return reg.IsMatch(email);
            }
            return true;
        }
    }
}
